package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.user.article.ArticleListener;
import com.prp.gwt.client.vo.CommentList;
import com.prp.gwt.client.vo.SubjectRecommend;

public class RecommendPanel extends VerticalPanel {

	private static RecommendPanel instance;
	private VerticalPanel recommendPanel = new VerticalPanel();
	private VerticalPanel commentPanel = new VerticalPanel();
	SubjectRecommend recommend;
	private static Dictionary dictionary = Dictionary
			.getDictionary("constants");
	private int score = 10;

	public static RecommendPanel getInstance() {
		if (instance == null)
			instance = new RecommendPanel();
		return instance;
	}

	private RecommendPanel() {
		recommendPanel.addStyleName("recommend-item");
		commentPanel.addStyleName("recommend-comment");
		add(recommendPanel);
		add(commentPanel);
		addStyleName("recommend");
	}

	public void init() {
		recommendPanel.clear();
		commentPanel.clear();
	}

	public void loadRecommend(SubjectRecommend recommend) {
		this.recommend = recommend;
		recommendPanel.clear();
		HorizontalPanel innerPanel = new HorizontalPanel();
		// innerPanel.addStyleName("recommend-item");
		Image img = new Image(recommend.getUser().getPortrait());
		img.addStyleName("recommend-item-image");
		innerPanel.add(img);
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.addStyleName("recommend-item-content");
		HorizontalPanel titlePanel = new HorizontalPanel();
		HTML title = new HTML("[" + recommend.getCategoryName() + "]["
				+ recommend.getCategoryChildName() + "]" + "<a>"
				+ recommend.getTitle() + "</a>");
		title.addStyleName("recommend-item-title");
		titlePanel.add(title);
		vPanel.add(titlePanel);
		HorizontalPanel panel = new HorizontalPanel();
		Label label = new Label("推荐:" + recommend.getEntityName());
		label.addStyleName("recommend-item-label");
		panel.add(label);
		label = new Label("作者:" + recommend.getUser().getUsername());
		label.addStyleName("recommend-item-label");
		panel.add(label);
		if (recommend.getDate() != null) {
			label = new Label("日期:" + recommend.getDate());
			label.addStyleName("recommend-item-label");
			panel.add(label);
		}
		vPanel.add(panel);
		HTML content = new HTML(recommend.getContent());
		content.addStyleName("recommend-item-text");
		vPanel.add(content);
		panel = new HorizontalPanel();
		label = new Label("点击(" + recommend.getClickFreq() + ")");
		label.addStyleName("recommend-item-label");
		panel.add(label);
		label = new Label("评论(" + recommend.getReplyNum() + ")");
		label.addStyleName("recommend-item-label");
		panel.add(label);
		{
			HorizontalPanel scorePanel = new HorizontalPanel();
			scorePanel.addStyleName("recommend-score");
			label = new Label("评分:");
			label.addStyleName("score-text");
			scorePanel.add(label);
			Label score = new Label(recommend.getScore() + "分");
			score.addStyleName("star" + recommend.getScore().intValue());
			scorePanel.add(score);
			score.setTitle(recommend.getScore() + "分");
			panel.add(scorePanel);
		}
		vPanel.add(panel);
		innerPanel.add(vPanel);
		recommendPanel.add(innerPanel);
	}

	public void loadCommentList(CommentList commentList) {
		commentPanel.clear();
		HorizontalPanel tPanel = new HorizontalPanel();
		tPanel.addStyleName("recommend-comment-title");
		HTML html = new HTML("评论");
		html.addStyleName("recommend-comment-title-text");
		tPanel.add(html);
		commentPanel.add(tPanel);
		for (int i = 0; i < commentList.size(); i++)
			commentPanel.add(new CommentItem(commentList.get(i)));
		loadAddCommentPanel();
	}

	private void loadAddCommentPanel() {
		HTML html = new HTML("添加评论:");
		commentPanel.add(html);
		html.addStyleName("comment-add-label");
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.addStyleName("comment-add-row");
			html = new HTML("评价:");
			html.addStyleName("comment-add-text");
			hPanel.add(html);
			final Label points = new Label("10分");
			points.addStyleName("comment-add-text");
			final Image[] images = new Image[10];
			for (int i = 0; i < images.length; i++) {
				images[i] = new Image(Host.getHOST()
						+ dictionary.get(ClientConstants.STAR));
				hPanel.add(images[i]);
				final int k = i;
				images[i].addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						score = k + 1;
						points.setText(score + "分");
					}
				});
				images[i].addMouseOverHandler(new MouseOverHandler() {
					@Override
					public void onMouseOver(MouseOverEvent event) {
						for (int j = 0; j < k + 1; j++)
							images[j].setUrl(Host.getHOST()
									+ dictionary.get(ClientConstants.STAR));
						for (int j = k + 1; j < images.length; j++)
							images[j].setUrl(Host.getHOST()
									+ dictionary.get(ClientConstants.NSTAR));
					}
				});
				images[i].addMouseOutHandler(new MouseOutHandler() {
					@Override
					public void onMouseOut(MouseOutEvent event) {
						int x = event.getClientX();
						int y = event.getClientY();
						if (x < images[0].getAbsoluteLeft()
								|| x > images[images.length - 1]
										.getAbsoluteLeft()
										+ images[0].getWidth()
								|| y < images[0].getAbsoluteTop()
								|| y > images[0].getAbsoluteTop()
										+ images[0].getHeight()) {
							for (int j = 0; j < score; j++)
								images[j].setUrl(Host.getHOST()
										+ dictionary.get(ClientConstants.STAR));
							for (int j = score; j < images.length; j++)
								images[j]
										.setUrl(Host.getHOST()
												+ dictionary
														.get(ClientConstants.NSTAR));
						}
					}
				});
			}
			hPanel.add(points);
			commentPanel.add(hPanel);
		}
		final TextArea textArea = new TextArea();
		textArea.addStyleName("comment-add-area");
		textArea.setCharacterWidth(60);
		textArea.setVisibleLines(7);
		commentPanel.add(textArea);
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.addStyleName("comment-add-row");
			Label label = new Label("请限制在500字以内");
			label.addStyleName("comment-add-text");
			hPanel.add(label);
			final Label word = new Label("0");
			textArea.addKeyUpHandler(new KeyUpHandler() {
				@Override
				public void onKeyUp(KeyUpEvent event) {
					word.setText("" + textArea.getText().length());
				}
			});
			hPanel.add(word);
			hPanel.add(new Label("/500"));
			commentPanel.add(hPanel);
		}
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			Button commit = new Button("提交");
			hPanel.addStyleName("comment-add-button");
			hPanel.add(commit);
			commentPanel.add(hPanel);
			commit.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					String str = textArea.getValue();
					if (str.length() > 500) {
						Window.alert("超过字数将被舍弃！");
						str = str.substring(0, 500);
					}
					ArticleListener.getInstance().addComment(recommend.getId(),
							str, score);
				}
			});
		}
	}
}
